/* ********************************************************************************* */
/* 5 ************************* Mergers & Acquisitions ****************************** */
/* ********************************************************************************* */
/* Summary   : Code builds firm year panel sample to estimtate the probability of    */
/* 			   making an M&A bid and an M&A sample for deal performance tests        */                                              
/* 			   M&A data from SDC                                                     */                                              
/* ********************************************************************************* */

	LIBNAME cow 'C:\cow';
	LIBNAME data 'C:\data';


/* ************************************** */
/* ****** M&A sample at deal level ****** */
/* ************************************** */



*** get M&A sample from SDC during 1970-2018, save as SAS file MA19702018;
	data MA19812018; set data.MA19702018; if year(ad)>=1981;
	if form in ("Merger","Acquisition","Acq. Maj. Int.") and ed~=.;
	run;

* download CRSP/Compustat merged header file from WRDS CRSP/Compustat Merged - Fundamentals Annual;
* https://wrds-www.wharton.upenn.edu/pages/get-data/center-research-security-prices-crsp/annual-update/crspcompustat-merged/fundamentals-annual/ ; 
* get CRSP/Comp headers into SDC sample;
	proc sort data=data.headercrspcomp; by cusip;  run;
	data t_header; set data.headercrspcomp; 
	if LINKENDDT>0 then t_ENDDT=LINKENDDT; else t_ENDDT=mdy(12,31,2021); t_BEGDT=LINKDT; 
	t_cusip=substr(cusip,1,6); t_gvkey=gvkey*1; t_npermno=lpermno; t_fyenddt=datadate; t_cik=cik; t_fyear=fyear; t_sic=sic*1;
	format t_BEGDT t_ENDDT t_fyenddt date9.; keep t_:; proc sort nodup; by t_cusip; run; 
	data a_header; set data.headercrspcomp; 
	if LINKENDDT>0 then a_ENDDT=LINKENDDT; else a_ENDDT=mdy(12,31,2021); a_BEGDT=LINKDT; 
	a_cusip=substr(cusip,1,6); a_gvkey=gvkey*1; a_npermno=lpermno; a_fyenddt=datadate; a_cik=cik; a_fyear=fyear; a_sic=sic*1;
	format a_BEGDT a_ENDDT a_fyenddt date9.; keep a_:; proc sort nodup; by a_cusip; run; 

	proc sql; create table sdc1	as select * from MA19812018 as a left join t_header as b on a.t_cusip= b.t_cusip ; quit; 
	data sdc1; set sdc1; diff=AD-t_fyenddt; if diff<0 then delete; proc sort; by deal_no diff; run;
	data sdc1; set sdc1; by deal_no; if first.deal_no; drop diff; run;

	proc sql; create table sdc2	as select * from MA19812018 as a left join a_header as b on a.a_cusip= b.a_cusip; quit; 
	data sdc2; set sdc2; diff=AD-a_fyenddt; if diff<0 then delete; proc sort; by deal_no diff; run;
	data sdc2; set sdc2; by deal_no; if first.deal_no; drop diff; run;
	
	data MA19812018header; merge sdc1(in=in1) sdc2(in=in2); by deal_no; 
	drop t_BEGDT t_ENDDT a_BEGDT a_ENDDT; run; proc sort; by deal_no; run;
	proc sql; drop table sdc1, sdc2, t_header, a_header; quit;

	data data.MA19812018headerind; set MA19812018header;
	if (t_sdcsic>=100 & t_sdcsic<=799)or t_sdcsic=2048 then t_sdc_ind_name='agric';
	else if (t_sdcsic>=2000 & t_sdcsic<=2046) or (t_sdcsic>=2050 & t_sdcsic<=2063) or (t_sdcsic>=2070 & t_sdcsic<=2079)
		or (t_sdcsic>=2090 & t_sdcsic<=2095) or (t_sdcsic>=2098 & t_sdcsic<=2099) then t_sdc_ind_name='food';
	else if (t_sdcsic>=2064 & t_sdcsic<=2068) or (t_sdcsic>=2086 & t_sdcsic<=2087) or (t_sdcsic>=2096 & t_sdcsic<=2097)
		then t_sdc_ind_name='sod';
	else if (t_sdcsic>=2080 & t_sdcsic<=2085) then t_sdc_ind_name='beer';
	else if (t_sdcsic>=2100 & t_sdcsic<=2199)then t_sdc_ind_name='smoke';
	else if (t_sdcsic>=900 & t_sdcsic<=999) or (t_sdcsic>=3650 & t_sdcsic<=3652) or (t_sdcsic>=3732 & t_sdcsic<=3732)
		or (t_sdcsic>=3930 & t_sdcsic<=3949) then t_sdc_ind_name='toys';
	else if (t_sdcsic>=7800 & t_sdcsic<=7841) or (t_sdcsic>=7900 & t_sdcsic<=7999) then t_sdc_ind_name='fun';
	else if (t_sdcsic>=2700 & t_sdcsic<=2749) or (t_sdcsic>=2770 & t_sdcsic<=2799) then t_sdc_ind_name='books';
	else if (t_sdcsic>=2047 & t_sdcsic<=2047) or (t_sdcsic>=2391 & t_sdcsic<=2392) or (t_sdcsic>=2510 & t_sdcsic<=2519)
		or (t_sdcsic>=2590 & t_sdcsic<=2599) or (t_sdcsic>=2840& t_sdcsic<=2844) or (t_sdcsic>=3160 & t_sdcsic<=3199) 
		or (t_sdcsic>=3229 & t_sdcsic<=3231) or (t_sdcsic>=3260 & t_sdcsic<=3260)
		or (t_sdcsic>=3262 & t_sdcsic<=3263) or (t_sdcsic>=3269 & t_sdcsic<=3269) 
		or (t_sdcsic>=3630 & t_sdcsic<=3639) or (t_sdcsic>=3750 & t_sdcsic<=3751) or (t_sdcsic>=3800 & t_sdcsic<=3800)
		or (t_sdcsic>=3860 & t_sdcsic<=3879) or (t_sdcsic>=3910 & t_sdcsic<=3919) 
		or (t_sdcsic>=3960 & t_sdcsic<=3961) or (t_sdcsic>=3991 & t_sdcsic<=3991) or (t_sdcsic>=3995 & t_sdcsic<=3995)
		then t_sdc_ind_name='hshld';
	else if (t_sdcsic>=2300 & t_sdcsic<=2390) or (t_sdcsic>=3020 & t_sdcsic<=3021) or (t_sdcsic>=3100 & t_sdcsic<=3111)
		or (t_sdcsic>=3130 & t_sdcsic<=3159) or (t_sdcsic>=3965 & t_sdcsic<=3965) then t_sdc_ind_name='clths';
	else if (t_sdcsic>=8000 & t_sdcsic<=8099) then t_sdc_ind_name='hlth';
	else if (t_sdcsic>=3693 & t_sdcsic<=3693) or (t_sdcsic>=3840 & t_sdcsic<=3851) then t_sdc_ind_name='medeq';
	else if (t_sdcsic>=2830 & t_sdcsic<=2836) then t_sdc_ind_name='drugs';
	else if (t_sdcsic>=2800 & t_sdcsic<=2829) or (t_sdcsic>=2850 & t_sdcsic<=2899)then t_sdc_ind_name='chems';
	else if (t_sdcsic>=3000 & t_sdcsic<=3000) or (t_sdcsic>=3050 & t_sdcsic<=3099)then t_sdc_ind_name='rubbr';
	else if (t_sdcsic>=2200 & t_sdcsic<=2295) or (t_sdcsic>=2297 & t_sdcsic<=2299) or (t_sdcsic>=2393 & t_sdcsic<=2395)
		or (t_sdcsic>=2397 & t_sdcsic<=2399) then t_sdc_ind_name='txtls';
	else if (t_sdcsic>=800 & t_sdcsic<=899) or (t_sdcsic>=2400 & t_sdcsic<=2439) or (t_sdcsic>=2450 & t_sdcsic<=2459)
		or (t_sdcsic>=2490 & t_sdcsic<=2499) or (t_sdcsic>=2950 & t_sdcsic<=2952) 
		or (t_sdcsic>=3200 & t_sdcsic<=3219) or (t_sdcsic>=3240 & t_sdcsic<=3259) or (t_sdcsic>=3261 & t_sdcsic<=3261)
		or (t_sdcsic>=3264 & t_sdcsic<=3264) or (t_sdcsic>=3270 & t_sdcsic<=3299) 
		or (t_sdcsic>=3420 & t_sdcsic<=3442) or (t_sdcsic>=3446 & t_sdcsic<=3452) or (t_sdcsic>=3490 & t_sdcsic<=3499)
		or (t_sdcsic>=3996 & t_sdcsic<=3996) then t_sdc_ind_name='bldmt';
	else if (1500<=t_sdcsic and t_sdcsic<=1549) or (1600<=t_sdcsic and t_sdcsic<=1699) or ( 1700<=t_sdcsic and t_sdcsic<=1799) then t_sdc_ind_name='cnstr';
	else if (3300<=t_sdcsic and t_sdcsic<=3369) or (3390<=t_sdcsic and t_sdcsic<=3399) then t_sdc_ind_name='steel';
	else if (t_sdcsic=3400) or (3443<=t_sdcsic and t_sdcsic<=3444) or (3460<=t_sdcsic and t_sdcsic<=3479) then t_sdc_ind_name='fabpr';
	else if (3510<=t_sdcsic and t_sdcsic<=3536) or (3540<=t_sdcsic and t_sdcsic<=3569) or (3580<=t_sdcsic and t_sdcsic<=3599) then t_sdc_ind_name='mach';
	else if (3600<=t_sdcsic and t_sdcsic<=3621) or (3623<=t_sdcsic and t_sdcsic<=3629) or (3640<=t_sdcsic and t_sdcsic<=3646) or
			(3648<=t_sdcsic and t_sdcsic<=3649) or (3660<=t_sdcsic and t_sdcsic<=3660) or (3691<=t_sdcsic and t_sdcsic<=3692) or
			(3699<=t_sdcsic and t_sdcsic<=3699) then t_sdc_ind_name='elceq';
	else if (2296<=t_sdcsic and t_sdcsic<=2296) or (2396<=t_sdcsic and t_sdcsic<=2396) or (3010<=t_sdcsic and t_sdcsic<=3011) or
			(3537<=t_sdcsic and t_sdcsic<=3537) or (3647<=t_sdcsic and t_sdcsic<=3647) or (3694<=t_sdcsic and t_sdcsic<=3694) or
			(3700<=t_sdcsic and t_sdcsic<=3716) or (3790<=t_sdcsic and t_sdcsic<=3792) or (3799<=t_sdcsic and t_sdcsic<=3799) then t_sdc_ind_name='auto';
	else if (3720<=t_sdcsic and t_sdcsic<=3729) then t_sdc_ind_name='aero';
	else if (3730<=t_sdcsic and t_sdcsic<=3731) or (3740<=t_sdcsic and t_sdcsic<=3743) then t_sdc_ind_name='ships';
	else if (3480<=t_sdcsic and t_sdcsic<=3489) or (3760<=t_sdcsic and t_sdcsic<=3769) or (3795<=t_sdcsic and t_sdcsic<=3795) then t_sdc_ind_name='guns';
	else if (1040<=t_sdcsic and t_sdcsic<=1049) then t_sdc_ind_name='gold';
	else if (1000<=t_sdcsic and t_sdcsic<=1039) or (1060<=t_sdcsic and t_sdcsic<=1099) or (1400<=t_sdcsic and t_sdcsic<=1499) then t_sdc_ind_name='mines';
	else if (1200<=t_sdcsic and t_sdcsic<=1299) then t_sdc_ind_name='coal';
	else if (1310<=t_sdcsic and t_sdcsic<=1389) or (2900<=t_sdcsic and t_sdcsic<=2911) or (2990<=t_sdcsic and t_sdcsic<=2999) then t_sdc_ind_name='energy';
	else if (4900<=t_sdcsic and t_sdcsic<=4999) then t_sdc_ind_name='util';
	else if (4800<=t_sdcsic and t_sdcsic<=4899) then t_sdc_ind_name='telem';
	else if (7020<=t_sdcsic and t_sdcsic<=7021) or (7030<=t_sdcsic and t_sdcsic<=7039) or (7200<=t_sdcsic and t_sdcsic<=7212) or
			(7215<=t_sdcsic and t_sdcsic<=7299) or (7395<=t_sdcsic and t_sdcsic<=7395) or (7500<=t_sdcsic and t_sdcsic<=7500) or
			(7520<=t_sdcsic and t_sdcsic<=7549) or (7600<=t_sdcsic and t_sdcsic<=7699) or (8100<=t_sdcsic and t_sdcsic<=8199) or
			(8200<=t_sdcsic and t_sdcsic<=8299) or (8300<=t_sdcsic and t_sdcsic<=8399) or (8400<=t_sdcsic and t_sdcsic<=8499) or
			(8600<=t_sdcsic and t_sdcsic<=8699) or (8800<=t_sdcsic and t_sdcsic<=8899) then t_sdc_ind_name='persv';
	else if (2750<=t_sdcsic and t_sdcsic<=2759) or (3993<=t_sdcsic and t_sdcsic<=3993) or (7300<=t_sdcsic and t_sdcsic<=7372) or
			(7374<=t_sdcsic and t_sdcsic<=7394) or (7397<=t_sdcsic and t_sdcsic<=7397) or (7399<=t_sdcsic and t_sdcsic<=7399) or
			(7510<=t_sdcsic and t_sdcsic<=7519) or (8700<=t_sdcsic and t_sdcsic<=8748) or (8900<=t_sdcsic and t_sdcsic<=8999) then t_sdc_ind_name='bussv';
	else if (3570<=t_sdcsic and t_sdcsic<=3579) or (3680<=t_sdcsic and t_sdcsic<=3689) or (3695<=t_sdcsic and t_sdcsic<=3695) or
			(7373<=t_sdcsic and t_sdcsic<=7373) then t_sdc_ind_name='comps';
	else if (3622<=t_sdcsic and t_sdcsic<=3622) or (3661<=t_sdcsic and t_sdcsic<=3679) or (3810<=t_sdcsic and t_sdcsic<=3810) or
			(3812<=t_sdcsic and t_sdcsic<=3812) then t_sdc_ind_name='chips';
	else if (3811<=t_sdcsic and t_sdcsic<=3811) or (3820<=t_sdcsic and t_sdcsic<=3830) then t_sdc_ind_name='labeq';
	else if (2520<=t_sdcsic and t_sdcsic<=2549) or (2600<=t_sdcsic and t_sdcsic<=2639) or (2670<=t_sdcsic and t_sdcsic<=2699) or
			(2760<=t_sdcsic and t_sdcsic<=2761) or (3950<=t_sdcsic and t_sdcsic<=3955) then t_sdc_ind_name='paper';
	else if (2440<=t_sdcsic and t_sdcsic<=2449) or (2640<=t_sdcsic and t_sdcsic<=2659) or (3210<=t_sdcsic and t_sdcsic<=3221) or
			(3410<=t_sdcsic and t_sdcsic<=3412) then t_sdc_ind_name='boxes';
	else if (4000<=t_sdcsic and t_sdcsic<=4099) or (4100<=t_sdcsic and t_sdcsic<=4199) or (4200<=t_sdcsic and t_sdcsic<=4299) or
			(4400<=t_sdcsic and t_sdcsic<=4499) or (4500<=t_sdcsic and t_sdcsic<=4599) or (4600<=t_sdcsic and t_sdcsic<=4699) or
			(4700<=t_sdcsic and t_sdcsic<=4799) then t_sdc_ind_name='trans';
	else if (5000<=t_sdcsic and t_sdcsic<=5099) or (5100<=t_sdcsic and t_sdcsic<=5199) then t_sdc_ind_name='whlsl';
	else if (5200<=t_sdcsic and t_sdcsic<=5299) or (5300<=t_sdcsic and t_sdcsic<=5399) or (5400<=t_sdcsic and t_sdcsic<=5499) or
			(5500<=t_sdcsic and t_sdcsic<=5599) or (5600<=t_sdcsic and t_sdcsic<=5699) or (5700<=t_sdcsic and t_sdcsic<=5736) or
			(5900<=t_sdcsic and t_sdcsic<=5999) then t_sdc_ind_name='rtail';
	else if (5800<=t_sdcsic and t_sdcsic<=5813) or (5890<=t_sdcsic and t_sdcsic<=5890) or (7000<=t_sdcsic and t_sdcsic<=7019) or
			(7040<=t_sdcsic and t_sdcsic<=7049) or (7213<=t_sdcsic and t_sdcsic<=7213) then t_sdc_ind_name='meals';
	else if (6000<=t_sdcsic and t_sdcsic<=6099) or (6100<=t_sdcsic and t_sdcsic<=6199) then t_sdc_ind_name='banks';
	else if (6300<=t_sdcsic and t_sdcsic<=6399) or (6400<=t_sdcsic and t_sdcsic<=6411) then t_sdc_ind_name='insur';
 	else if (6500<=t_sdcsic and t_sdcsic<=6553) then t_sdc_ind_name='riest';
	else if (6200<=t_sdcsic and t_sdcsic<=6299) or (6700<=t_sdcsic and t_sdcsic<=6799) then t_sdc_ind_name='fin';
	else t_sdc_ind_name='others';

	if (a_sdcsic>=100 & a_sdcsic<=799)or a_sdcsic=2048 then a_sdc_ind_name='agric';
	else if (a_sdcsic>=2000 & a_sdcsic<=2046) or (a_sdcsic>=2050 & a_sdcsic<=2063) or (a_sdcsic>=2070 & a_sdcsic<=2079)
		or (a_sdcsic>=2090 & a_sdcsic<=2095) or (a_sdcsic>=2098 & a_sdcsic<=2099) then a_sdc_ind_name='food';
	else if (a_sdcsic>=2064 & a_sdcsic<=2068) or (a_sdcsic>=2086 & a_sdcsic<=2087) or (a_sdcsic>=2096 & a_sdcsic<=2097)
		then a_sdc_ind_name='sod';
	else if (a_sdcsic>=2080 & a_sdcsic<=2085) then a_sdc_ind_name='beer';
	else if (a_sdcsic>=2100 & a_sdcsic<=2199)then a_sdc_ind_name='smoke';
	else if (a_sdcsic>=900 & a_sdcsic<=999) or (a_sdcsic>=3650 & a_sdcsic<=3652) or (a_sdcsic>=3732 & a_sdcsic<=3732)
		or (a_sdcsic>=3930 & a_sdcsic<=3949) then a_sdc_ind_name='toys';
	else if (a_sdcsic>=7800 & a_sdcsic<=7841) or (a_sdcsic>=7900 & a_sdcsic<=7999) then a_sdc_ind_name='fun';
	else if (a_sdcsic>=2700 & a_sdcsic<=2749) or (a_sdcsic>=2770 & a_sdcsic<=2799) then a_sdc_ind_name='books';
	else if (a_sdcsic>=2047 & a_sdcsic<=2047) or (a_sdcsic>=2391 & a_sdcsic<=2392) or (a_sdcsic>=2510 & a_sdcsic<=2519)
		or (a_sdcsic>=2590 & a_sdcsic<=2599) or (a_sdcsic>=2840& a_sdcsic<=2844) or (a_sdcsic>=3160 & a_sdcsic<=3199) 
		or (a_sdcsic>=3229 & a_sdcsic<=3231) or (a_sdcsic>=3260 & a_sdcsic<=3260)
		or (a_sdcsic>=3262 & a_sdcsic<=3263) or (a_sdcsic>=3269 & a_sdcsic<=3269) 
		or (a_sdcsic>=3630 & a_sdcsic<=3639) or (a_sdcsic>=3750 & a_sdcsic<=3751) or (a_sdcsic>=3800 & a_sdcsic<=3800)
		or (a_sdcsic>=3860 & a_sdcsic<=3879) or (a_sdcsic>=3910 & a_sdcsic<=3919) 
		or (a_sdcsic>=3960 & a_sdcsic<=3961) or (a_sdcsic>=3991 & a_sdcsic<=3991) or (a_sdcsic>=3995 & a_sdcsic<=3995)
		then a_sdc_ind_name='hshld';
	else if (a_sdcsic>=2300 & a_sdcsic<=2390) or (a_sdcsic>=3020 & a_sdcsic<=3021) or (a_sdcsic>=3100 & a_sdcsic<=3111)
		or (a_sdcsic>=3130 & a_sdcsic<=3159) or (a_sdcsic>=3965 & a_sdcsic<=3965) then a_sdc_ind_name='clths';
	else if (a_sdcsic>=8000 & a_sdcsic<=8099) then a_sdc_ind_name='hlth';
	else if (a_sdcsic>=3693 & a_sdcsic<=3693) or (a_sdcsic>=3840 & a_sdcsic<=3851) then a_sdc_ind_name='medeq';
	else if (a_sdcsic>=2830 & a_sdcsic<=2836) then a_sdc_ind_name='drugs';
	else if (a_sdcsic>=2800 & a_sdcsic<=2829) or (a_sdcsic>=2850 & a_sdcsic<=2899)then a_sdc_ind_name='chems';
	else if (a_sdcsic>=3000 & a_sdcsic<=3000) or (a_sdcsic>=3050 & a_sdcsic<=3099)then a_sdc_ind_name='rubbr';
	else if (a_sdcsic>=2200 & a_sdcsic<=2295) or (a_sdcsic>=2297 & a_sdcsic<=2299) or (a_sdcsic>=2393 & a_sdcsic<=2395)
		or (a_sdcsic>=2397 & a_sdcsic<=2399) then a_sdc_ind_name='txtls';
	else if (a_sdcsic>=800 & a_sdcsic<=899) or (a_sdcsic>=2400 & a_sdcsic<=2439) or (a_sdcsic>=2450 & a_sdcsic<=2459)
		or (a_sdcsic>=2490 & a_sdcsic<=2499) or (a_sdcsic>=2950 & a_sdcsic<=2952) 
		or (a_sdcsic>=3200 & a_sdcsic<=3219) or (a_sdcsic>=3240 & a_sdcsic<=3259) or (a_sdcsic>=3261 & a_sdcsic<=3261)
		or (a_sdcsic>=3264 & a_sdcsic<=3264) or (a_sdcsic>=3270 & a_sdcsic<=3299) 
		or (a_sdcsic>=3420 & a_sdcsic<=3442) or (a_sdcsic>=3446 & a_sdcsic<=3452) or (a_sdcsic>=3490 & a_sdcsic<=3499)
		or (a_sdcsic>=3996 & a_sdcsic<=3996) then a_sdc_ind_name='bldmt';
	else if (1500<=a_sdcsic and a_sdcsic<=1549) or (1600<=a_sdcsic and a_sdcsic<=1699) or ( 1700<=a_sdcsic and a_sdcsic<=1799) then a_sdc_ind_name='cnstr';
	else if (3300<=a_sdcsic and a_sdcsic<=3369) or (3390<=a_sdcsic and a_sdcsic<=3399) then a_sdc_ind_name='steel';
	else if (a_sdcsic=3400) or (3443<=a_sdcsic and a_sdcsic<=3444) or (3460<=a_sdcsic and a_sdcsic<=3479) then a_sdc_ind_name='fabpr';
	else if (3510<=a_sdcsic and a_sdcsic<=3536) or (3540<=a_sdcsic and a_sdcsic<=3569) or (3580<=a_sdcsic and a_sdcsic<=3599) then a_sdc_ind_name='mach';
	else if (3600<=a_sdcsic and a_sdcsic<=3621) or (3623<=a_sdcsic and a_sdcsic<=3629) or (3640<=a_sdcsic and a_sdcsic<=3646) or
			(3648<=a_sdcsic and a_sdcsic<=3649) or (3660<=a_sdcsic and a_sdcsic<=3660) or (3691<=a_sdcsic and a_sdcsic<=3692) or
			(3699<=a_sdcsic and a_sdcsic<=3699) then a_sdc_ind_name='elceq';
	else if (2296<=a_sdcsic and a_sdcsic<=2296) or (2396<=a_sdcsic and a_sdcsic<=2396) or (3010<=a_sdcsic and a_sdcsic<=3011) or
			(3537<=a_sdcsic and a_sdcsic<=3537) or (3647<=a_sdcsic and a_sdcsic<=3647) or (3694<=a_sdcsic and a_sdcsic<=3694) or
			(3700<=a_sdcsic and a_sdcsic<=3716) or (3790<=a_sdcsic and a_sdcsic<=3792) or (3799<=a_sdcsic and a_sdcsic<=3799) then a_sdc_ind_name='auto';
	else if (3720<=a_sdcsic and a_sdcsic<=3729) then a_sdc_ind_name='aero';
	else if (3730<=a_sdcsic and a_sdcsic<=3731) or (3740<=a_sdcsic and a_sdcsic<=3743) then a_sdc_ind_name='ships';
	else if (3480<=a_sdcsic and a_sdcsic<=3489) or (3760<=a_sdcsic and a_sdcsic<=3769) or (3795<=a_sdcsic and a_sdcsic<=3795) then a_sdc_ind_name='guns';
	else if (1040<=a_sdcsic and a_sdcsic<=1049) then a_sdc_ind_name='gold';
	else if (1000<=a_sdcsic and a_sdcsic<=1039) or (1060<=a_sdcsic and a_sdcsic<=1099) or (1400<=a_sdcsic and a_sdcsic<=1499) then a_sdc_ind_name='mines';
	else if (1200<=a_sdcsic and a_sdcsic<=1299) then a_sdc_ind_name='coal';
	else if (1310<=a_sdcsic and a_sdcsic<=1389) or (2900<=a_sdcsic and a_sdcsic<=2911) or (2990<=a_sdcsic and a_sdcsic<=2999) then a_sdc_ind_name='energy';
	else if (4900<=a_sdcsic and a_sdcsic<=4999) then a_sdc_ind_name='util';
	else if (4800<=a_sdcsic and a_sdcsic<=4899) then a_sdc_ind_name='telem';
	else if (7020<=a_sdcsic and a_sdcsic<=7021) or (7030<=a_sdcsic and a_sdcsic<=7039) or (7200<=a_sdcsic and a_sdcsic<=7212) or
			(7215<=a_sdcsic and a_sdcsic<=7299) or (7395<=a_sdcsic and a_sdcsic<=7395) or (7500<=a_sdcsic and a_sdcsic<=7500) or
			(7520<=a_sdcsic and a_sdcsic<=7549) or (7600<=a_sdcsic and a_sdcsic<=7699) or (8100<=a_sdcsic and a_sdcsic<=8199) or
			(8200<=a_sdcsic and a_sdcsic<=8299) or (8300<=a_sdcsic and a_sdcsic<=8399) or (8400<=a_sdcsic and a_sdcsic<=8499) or
			(8600<=a_sdcsic and a_sdcsic<=8699) or (8800<=a_sdcsic and a_sdcsic<=8899) then a_sdc_ind_name='persv';
	else if (2750<=a_sdcsic and a_sdcsic<=2759) or (3993<=a_sdcsic and a_sdcsic<=3993) or (7300<=a_sdcsic and a_sdcsic<=7372) or
			(7374<=a_sdcsic and a_sdcsic<=7394) or (7397<=a_sdcsic and a_sdcsic<=7397) or (7399<=a_sdcsic and a_sdcsic<=7399) or
			(7510<=a_sdcsic and a_sdcsic<=7519) or (8700<=a_sdcsic and a_sdcsic<=8748) or (8900<=a_sdcsic and a_sdcsic<=8999) then a_sdc_ind_name='bussv';
	else if (3570<=a_sdcsic and a_sdcsic<=3579) or (3680<=a_sdcsic and a_sdcsic<=3689) or (3695<=a_sdcsic and a_sdcsic<=3695) or
			(7373<=a_sdcsic and a_sdcsic<=7373) then a_sdc_ind_name='comps';
	else if (3622<=a_sdcsic and a_sdcsic<=3622) or (3661<=a_sdcsic and a_sdcsic<=3679) or (3810<=a_sdcsic and a_sdcsic<=3810) or
			(3812<=a_sdcsic and a_sdcsic<=3812) then a_sdc_ind_name='chips';
	else if (3811<=a_sdcsic and a_sdcsic<=3811) or (3820<=a_sdcsic and a_sdcsic<=3830) then a_sdc_ind_name='labeq';
	else if (2520<=a_sdcsic and a_sdcsic<=2549) or (2600<=a_sdcsic and a_sdcsic<=2639) or (2670<=a_sdcsic and a_sdcsic<=2699) or
			(2760<=a_sdcsic and a_sdcsic<=2761) or (3950<=a_sdcsic and a_sdcsic<=3955) then a_sdc_ind_name='paper';
	else if (2440<=a_sdcsic and a_sdcsic<=2449) or (2640<=a_sdcsic and a_sdcsic<=2659) or (3210<=a_sdcsic and a_sdcsic<=3221) or
			(3410<=a_sdcsic and a_sdcsic<=3412) then a_sdc_ind_name='boxes';
	else if (4000<=a_sdcsic and a_sdcsic<=4099) or (4100<=a_sdcsic and a_sdcsic<=4199) or (4200<=a_sdcsic and a_sdcsic<=4299) or
			(4400<=a_sdcsic and a_sdcsic<=4499) or (4500<=a_sdcsic and a_sdcsic<=4599) or (4600<=a_sdcsic and a_sdcsic<=4699) or
			(4700<=a_sdcsic and a_sdcsic<=4799) then a_sdc_ind_name='trans';
	else if (5000<=a_sdcsic and a_sdcsic<=5099) or (5100<=a_sdcsic and a_sdcsic<=5199) then a_sdc_ind_name='whlsl';
	else if (5200<=a_sdcsic and a_sdcsic<=5299) or (5300<=a_sdcsic and a_sdcsic<=5399) or (5400<=a_sdcsic and a_sdcsic<=5499) or
			(5500<=a_sdcsic and a_sdcsic<=5599) or (5600<=a_sdcsic and a_sdcsic<=5699) or (5700<=a_sdcsic and a_sdcsic<=5736) or
			(5900<=a_sdcsic and a_sdcsic<=5999) then a_sdc_ind_name='rtail';
	else if (5800<=a_sdcsic and a_sdcsic<=5813) or (5890<=a_sdcsic and a_sdcsic<=5890) or (7000<=a_sdcsic and a_sdcsic<=7019) or
			(7040<=a_sdcsic and a_sdcsic<=7049) or (7213<=a_sdcsic and a_sdcsic<=7213) then a_sdc_ind_name='meals';
	else if (6000<=a_sdcsic and a_sdcsic<=6099) or (6100<=a_sdcsic and a_sdcsic<=6199) then a_sdc_ind_name='banks';
	else if (6300<=a_sdcsic and a_sdcsic<=6399) or (6400<=a_sdcsic and a_sdcsic<=6411) then a_sdc_ind_name='insur';
 	else if (6500<=a_sdcsic and a_sdcsic<=6553) then a_sdc_ind_name='riest';
	else if (6200<=a_sdcsic and a_sdcsic<=6299) or (6700<=a_sdcsic and a_sdcsic<=6799) then a_sdc_ind_name='fin';
	else a_sdc_ind_name='others';
	run;
	

	%macro winsor(dsetin=, dsetout=, byvar=, vars=, type=, pctl=);
	%if &dsetout = %then %let dsetout = &dsetin; %let varL=; %let varH=; %let xn=1;
	%do %until ( %scan(&vars,&xn)= ); %let token = %scan(&vars,&xn); %let varL = &varL &token.L; %let varH = &varH &token.H; %let xn=%EVAL(&xn + 1); %end;%let xn=%eval(&xn-1); data xtemp; set &dsetin; run;
	%if &byvar = none %then %do;		data xtemp; set xtemp; xbyvar = 1; run;    %let byvar = xbyvar;	%end;
	proc sort data = xtemp; by &byvar; run;	proc univariate data = xtemp noprint; by &byvar; var &vars;	output out = xtemp_pctl PCTLPTS = &pctl PCTLPRE = &vars PCTLNAME = L H; run;
	data &dsetout; merge xtemp xtemp_pctl; by &byvar;    array trimvars{&xn} &vars; array trimvarl{&xn} &varL; array trimvarh{&xn} &varH;
	do xi = 1 to dim(trimvars); %if &type = winsor %then %do; if not missing(trimvars{xi}) then do; if (trimvars{xi} < trimvarl{xi}) then trimvars{xi} = trimvarl{xi}; if (trimvars{xi} > trimvarh{xi}) then trimvars{xi} = trimvarh{xi}; end; %end;
    %else %do; if not missing(trimvars{xi}) then do; if (trimvars{xi} < trimvarl{xi}) then delete; if (trimvars{xi} > trimvarh{xi}) then delete; end;%end; end; drop &varL &varH xbyvar xi; run;
	%mend winsor;


* use Compustat data files comp for the target and a_comp for the acquirer 
  generated from SAS program file "0 Compustat.sas"
	
* acquirer compustat current year;
	data a_comp; set data.a_comp; if a_fyear>=1970; run; 
	proc sql; create table set1 as select * from data.MA19812018headerind as a left join a_comp as b on a.a_fyear=b.a_fyear and a.a_gvkey=b.a_gvkey; quit;
	proc sort; by deal_no descending a_shrout; run; data set1; set set1; by deal_no; if first.deal_no; run;

* target compustat current year;
	data t_comp; set data.comp; if fyear>=1970; run;
	proc sql; create table set2 as select * from set1 as a left join t_comp as b on a.t_fyear=b.fyear and a.t_gvkey=b.gvkey; quit;
	proc sort; by deal_no descending shrout; run; data set2; set set2; by deal_no; if first.deal_no; run;




*****************************************************;
* Calculate abnormal returns around deal announcement;
* market model CAR (-1,1) using (-525,-273)          ;
*****************************************************;


* obtain the text file for event study that has permno (acquirer permno) and event date (deal announcement date);
	data a_npermno; set data.MA19812018headerind; if a_npermno~=.; perm=a_npermno; anndate=ad; format anndate yymmddn8.; keep perm anndate; run;
	PROC EXPORT DATA=a_npermno DBMS=tab	OUTFILE='C:\data\a_npermno.txt'	REPLACE;PUTNAMES=NO;RUN;

* get returns from CRSP http://wrds-web.wharton.upenn.edu/ds/crsp/dstk/dsfwin/ 
  daily extract (-525,252) save as SAS data file as a_return;


* market model regressions ; 
	proc sort data=data.a_return; by event_date permno td_count; run;
	data estwindow; set data.a_return; if -525<=TD_COUNT<=-273; run;
	proc sort; by event_date permno td_count; run;
	proc reg noprint data=estwindow outest=mm; by event_date permno;
	model RET = VWRETD; quit;
	data mm; set mm; alpha=intercept; beta=VWRETD; 
	keep event_date permno alpha beta; run;
	proc sql; create table returnmm as select * from data.a_return as a left join mm as b 
    on a.event_date=b.event_date and a.permno=b.permno;	quit;
	proc sort data=returnmm; by event_date permno td_count; run;

* get deal_no into returns;
	data samplekink; set data.MA19812018headerind; run; proc sort nodup; by deal_no AD a_npermno ; run;
	data td_count; set data.a_return; keep td_count; run; proc sort nodup; by td_count; run;
	data id; set samplekink; event_date=AD; permno=a_npermno; format event_date date9.; 
	keep deal_no event_date permno; run;
	proc sort nodup; by deal_no; run;
	proc sql; create table joint as select * from id, td_count; quit;
	proc sql; create table a_allreturn as select * from joint as a left join returnmm as b 
	on a.event_date=b.event_date and a.permno=b.permno and a.td_count=b.td_count; quit;
	data a_allreturn; set a_allreturn; if prc<0 then prc=prc*(-1); if prc=. then ret=0; if alpha=. then alpha=0; if beta=. then beta=1; run;

* calculate market model adjusted returns;
	%macro CAR(in=, out=, start=, end=);
		%local in out start end;
		data abnret; set &in; if &start <= TD_COUNT <= &end; 
		AR = RET - alpha - beta*VWRETD ; run;
		proc sort data = abnret; by deal_no; run;
		proc means data = abnret noprint; by deal_no; 
		output out = abnretout sum = CAR; var AR; run;
		data &out (drop= _type_ _freq_); set abnretout; rename CAR = &out; run;
	%mend CAR;

* calculate market adjusted returns;
	%macro CAR_R(in=, out=, start=, end=);
		%local in out start end;
		data abnret; set &in; if &start <= TD_COUNT <= &end; 
		AR = RET - VWRETD ; run;
		proc sort data = abnret; by deal_no; run;
		proc means data = abnret noprint; by deal_no; 
		output out = abnretout sum = CAR; var AR; run;
		data &out (drop= _type_ _freq_); set abnretout; rename CAR = &out; run;
	%mend CAR_R;

	%CAR(in=a_allreturn, out=a_CAR0101, start=-1, end=1);
	%CAR_R(in=a_allreturn, out=a_CAR0101_R, start=-1, end=1);

* acquirer market cap 2 days before announcement;
	data a_mktcap_2; set a_allreturn; 
	if td_count=-2; a_mktcap_2=prc*shrout/1000;
	keep deal_no a_mktcap_2; proc sort; by deal_no; run;

* merge CAR(-1,+1) into M&A sample;
 	data setm; merge set2(in=in1) a_CAR0101 a_CAR0101_R a_mktcap_2; by deal_no; if in1; run;



**************************;
* Amihud liquidity measure;
**************************;

	data data.base; set data.comp; datadate=fyenddt; permno=LPERMNO; 
	format datadate date9.; keep datadate cusip permno id; run;


	OPTIONS NOLABEL NOCENTER NONUMBER NODATE linesize=220 pagesize=150 SYMBOLGEN MPRINT ERRORS=2;

	LIBNAME data 'C:\cow';
	libname crsp '/wrds/crsp/sasdata/a_stock';

	%LET wrds=wrds.wharton.upenn.edu 4016;
	OPTIONS COMAMID=TCP REMOTE=WRDS;
	SIGNON USERNAME=_prompt_;

	RSUBMIT;

	data crsp; set crsp.dsf; 
	if ret~=. and ret~=0 and prc > 0;
	illiq=1000000*abs(ret)/(abs(prc)*vol);
	keep date illiq permno cusip; 
	run;

	PROC UPLOAD DATA=data.base OUT=base;RUN;

	proc sql; create table z2 as select * from base as a left join crsp as b 
	on substr(a.cusip,1,6)=substr(b.cusip,1,6) and mdy(month(a.datadate),day(a.datadate),year(a.datadate)-1)<b.date<=a.datadate; quit;

	proc sort data=z2; by id cusip datadate;
	proc means noprint data=z2; by id cusip datadate; var illiq; 
	output out=illiq_cusip (drop=_:) mean=; run;

	PROC DOWNLOAD DATA=illiq_cusip OUT=data.illiq_cusip;RUN;

	ENDRSUBMIT;

	* upload to WRDS and run: 
	wrds(~)% cd /home/city/anhtran
	wrds(~)% nohup sas amihud.sas &
	;

* get liquidity into M&A sample;
	data a_illiq; set data.illiq; a_amihudliq=illiq*(-1); a_npermno=permno; a_fyenddt=datadate; keep a_:; run;

	proc sql; create table set12 as select * from setm as a left join a_illiq as b 
	on a.a_npermno=b.a_npermno and a.a_fyenddt=b.a_fyenddt; quit;
	proc sort data=set12; by deal_no descending amihudliq; run;
	data set12; set set12; by deal_no; if first.deal_no; run;


* final M&A sample;
	data data.ma19802018final; set set12; run;


*** get state into M&A ***;

	data ma19802018sample; set data.ma19802018final; run;

* use firm-year panel data set from SAS program file "1 mvc.sas";
	data a_state; set cow.mvc_data; a_gvkey=gvkey*1; a_fyear=fyear; a_incorp=incorp; a_state=state;
	keep a_gvkey a_fyear a_incorp a_state ; run; 
	data t_state; set cow.mvc_data; t_gvkey=gvkey*1; t_fyear=fyear; t_incorp=incorp; t_state=state;
	keep t_gvkey t_fyear t_incorp t_state ; run; 


	proc sql; create table zemp1 as select * from ma19802018sample as a left join a_state as b 
	on a.a_gvkey= b.a_gvkey and a.a_fyear=b.a_fyear; quit;
	proc sql; create table zemp2 as select * from zemp1 as a left join t_state as b 
	on a.t_gvkey= b.t_gvkey and a.t_fyear=b.t_fyear; quit;
		
	proc sort data=zemp2; by deal_no; run;
	data cow.masample; set zemp2; by deal_no; if first.deal_no; run;



* Process M&A variables; 

	* M&A sample;
	data test; set cow.masample; 
	
	if attitude="Hostile" then D_hostile=1; else D_hostile=0; 
	if attitude="Friendly" then D_friendly=1; else D_friendly=0; 
	if pmt="CASHO" then D_allcash=1; else D_allcash=0; if pct_cash=. then pct_cash=100-pct_stock; 
	if pmt in ("SHARES") then D_allstock=1; else D_allstock=0; 
	if toeholdpct>=5 then D_toehold=1; else D_toehold=0;
	if toeholdpct>0 then D_toehold0=1; else D_toehold0=0;
	if toeholdpct=. then toeholdpct=0;
	if d_tlockup=1 then d_lockup=1; else d_lockup=0;
	if t_sdc_ind_name=a_sdc_ind_name then D_relate=1; else D_relate=0;
	if floor(a_sdcsic/100)=floor(t_sdcsic/100) then D_same2sic=1; else D_same2sic=0;
	l_dealvalue = log(1+dealvalue);
	rel_size=dealvalue/a_mktcap_2;

	if a_public="Public" then D_a_public=1; else D_a_public=0;
	if a_public="Priv." then D_a_private=1; else D_a_private=0;
	if a_public="Sub." then D_a_sub=1; else D_a_sub=0;

	l_a_mktcap=log(1+a_mktcap);
	l_a_assetsm=log(1+a_assetsm);
	l_a_assets=log(1+a_assets);
		
	if t_public="Public" then D_t_public=1; else D_t_public=0;
	if t_public="Public" then D_t_privsub=0; else D_t_privsub=1;
	if t_public="Priv." then D_t_private=1; else D_t_private=0;
	if t_public="Sub." then D_t_sub=1; else D_t_sub=0;

	if a_car0101<0 then a_car0101_neg=1; else a_car0101_neg=0; 
	if a_car0101_r<0 then a_car0101_r_neg=1; else a_car0101_r_neg=0; 

	a_ind1d=floor(a_sic/1000);
	a_ind1d_a_fyear=a_ind1d+a_fyear*100;
	a_ind1d_yearad=a_ind1d+yearad*100;
	a_ind2d=floor(a_sic/100);
	a_ind2d_a_fyear=a_ind2d+a_fyear*100;
	a_ind2d_yearad=a_ind2d+yearad*100;
	a_ind3d=floor(a_sic/10);
	a_ind3d_a_fyear=a_ind3d+a_fyear*1000;
	a_ind3d_yearad=a_ind3d+yearad*1000;
	a_state_id=stfips(a_state); t_state_id=stfips(t_state);
	a_incorp_id=stfips(a_incorp); t_incorp_id=stfips(t_incorp); 
	a_cusip_id=stfips(a_cusip); 
	a_state_yearad=yearad*100+a_state_id; 
	a_state_a_fyear=a_fyear*100+a_state_id; 
	a_state_a_ind2d=a_ind2d*100+a_state_id; 
	a_incorp_yearad=yearad*100+a_incorp_id; 
	a_incorp_a_fyear=a_fyear*100+a_incorp_id; 
	a_incorp_a_ind2d=a_ind2d*100+a_incorp_id; 
	run;

	proc datasets nolist; modify test; attrib _all_ label=''; quit; run;

	proc sort; by deal_no; run;
	


	%macro prefixvars(inpdsn,prefix,outdsn,excludevars=); %let num=1;	%let excludevar=%scan(%upcase(&excludevars),&num,' ');
	%let excludevar&num=&excludevar; %do %while(&excludevar ne ); %let num=%eval(&num + 1);	%let excludevar=%scan(&excludevars,&num,' ');
	%let excludevar&num=&excludevar; %end;	%let numkeyvars=%eval(&num - 1); 	%let dsid=%sysfunc(open(&inpdsn)); 	%let numvars=%sysfunc(attrn(&dsid,nvars)); 	
	data &outdsn;	set &inpdsn(rename=(%do i = 1 %to &numvars;	%let flag=N;	%let var&i=%sysfunc(varname(&dsid,&i));	%do j=1 %to &numkeyvars;
	%if %upcase(&&var&i) eq &&excludevar&j %then %let flag=Y;	%end;	%if &flag eq N %then %do; &&var&i=&prefix&&var&i %end;	%end;));
	%let rc=%sysfunc(close(&dsid));	run;	%mend prefixvars;
	%prefixvars(mvc_all,a_,a_mvc_all,excludevars=)

	proc sql; create table testma_all as select * from test as a left join a_mvc_all as b 
	on a.a_fyear=b.a_fyear and a.a_npermno=b.a_permno; quit;

	PROC EXPORT DATA= testma_all OUTFILE= "C:\cow\ma.dta" DBMS=STATA REPLACE;RUN;


/* ************************************************************ */
/* ****** Firm year sample to estimate prob making a bid ****** */
/* ************************************************************ */

	data ma; set data.ma19802018final; a_fyear=year(ad); bid=1; keep a_fyear a_cusip bid dealvalue ; 
	proc sort ; by a_fyear a_cusip bid ; run;
	proc means noprint data=ma; by a_fyear a_cusip bid; var dealvalue; 
	output out=ma_value (drop=_type_) sum=; run;

	* use mvc_all SAS data file as the firm-year panel from SAS program "2 mvc.sas";
	proc sql; create table comp_ma_temp as select * from mvc_all as a left join ma_value as b 
	on a.fyear=b.a_fyear and substr(a.cusip,1,6)=b.a_cusip; quit;

	* get size and ROA from "0 compustat.sas";
	proc sql; create table comp_ma_temp_1 as select * from comp_ma_temp as a left join data.comp as b 
	on a.gvkey=b.gvkey and a.fyear=b.fyear; quit; 

	* get Amihud liquidity into panel;
	data illiq_cusip; set data.illiq_cusip; keep cusip datadate illiq; run;
	proc sql; create table comp_ma as select * from comp_ma_temp_2 as a left join illiq_cusip as b 
	on a.datadate=b.datadate and a.cusip=b.cusip; quit;
	proc sort; by row descending illiq; run;

	data comp_ma; set comp_ma; by row; if first.row;
	if bid=. then bid=0; 
	if _freq_=. then _freq_=0; bidcount=_freq_; 
	if dealvalue=. then dealvalue=0; l_dealvalue=log(1+dealvalue); 
	l_bidcount=log(1+bidcount); ind1d=floor(sic/1000); ind1d_fyear=ind1d+fyear*100; 
	ind2d=floor(sic/100);	ind2d_fyear=ind2d+fyear*100;
	run;

	PROC EXPORT DATA= comp_ma OUTFILE= "C:\cow\comp_ma.dta" DBMS=STATA REPLACE;RUN;

